#include <stdio.h>		/* fprintf() */
#include <unistd.h>		/* */
#include <sys/types.h>

#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
#ifndef MAXHOSTNAMELEN
#define	MAXHOSTNAMELEN	256
#endif

void ast_copy_string(char *dst, const char *src, size_t size)
{
	while (*src && size) {
		*dst++ = *src++;
		size--;
	}
	if (__builtin_expect(!size, 0))
		dst--;
	*dst = '\0';
}

static char *_argv[256];
pid_t ast_mainpid;


int main(int argc, char *argv[])
{
	int x;
	char filename[80] = "";
	char hostname[MAXHOSTNAMELEN] = "";
	int isroot = 1;
	
	uid_t current;	

	/* Remember original args for restart */
	if (argc > ARRAY_LEN(_argv) - 1) {
		fprintf(stderr, "Truncating argument size to %d\n", (int)ARRAY_LEN(_argv) - 1);
		argc = ARRAY_LEN(_argv) - 1;
	}
	for (x = 0; x < argc; x++) {
		_argv[x] = argv[x];
		fprintf(stdout, "argv[%d]=%s\n", x, argv[x]);
	}
	_argv[x] = NULL;
	
	if ((current = geteuid()) != 0) {
		isroot = 0;
		fprintf(stdout, "current userid is %d\n", current);
	}
	
	if (argv[0] && (strstr(argv[0], "wxy")) != NULL) {
		 fprintf(stdout, "argv[0] is: %s\n", argv[0]);
	}

	if (gethostname(hostname, sizeof(hostname)-1))
		ast_copy_string(hostname, "<Unknown>", sizeof(hostname));

	for (x = 0; x < sizeof(hostname)-1; x++) {
        fprintf(stdout, "hostname[%d]=%c\n", x, hostname[x]);
    }
	
	ast_mainpid = getpid();
    fprintf(stdout, "pid is %ld\n", ast_mainpid);

	if (getenv("HOME"))
		snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));	
	for (x = 0; x < sizeof(filename)-1; x++) {
        fprintf(stdout, "filename[%d]=%c\n", x, filename[x]);
    }

	return 0;
}
